import time
from datetime import datetime,timedelta
import threading
import asyncio
from math import ceil
import socket

async def now3():
    await asyncio.sleep(1)
    host = "49.232.134.204"

    s = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)

    l = [-29 + 256] + [0 for i in range(47)]

    timestamp = ceil(time.time()) + 2208988800

    for x in range(4):
        l[43 - x] = timestamp % 256
        timestamp //= 256

    s.sendto(bytes(bytearray(l)), (host, 124))

    msg, _ = s.recvfrom(48)

    timestamp = 0
    for x in msg[40:44]:
        timestamp = 256 * timestamp + x

    timestamp -= 2208988800
    print(datetime.utcfromtimestamp(timestamp) + timedelta(hours=8))

    await asyncio.sleep(1)


async def run():
    start = time.time()
    await now3()
    print(datetime.now())
    print('cost %fs' % (time.time()-start))

l = []
async def main():
    for i in range(100):
        l.append(asyncio.create_task(run()))
    for c in l:
        await c

asyncio.run(main())

# 传播延迟取稳定平均值

# 消除魔数 2208988800

# timestamp 小数点后六位